<?php

namespace Modules\Silder\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Routing\Controller;
use Modules\Silder\Entities\Silder;

class SilderController extends Controller
{
    /**
     * Display a listing of the resource.
     * @return Response
     */
    public function index()
    {
        $silder = Silder::paginate(15);
        foreach ($silder as $v){
            $v['images'] = json_decode($v['images']);
        }
        return view('silder::silder.index',compact('silder'));
    }

    /**
     * Show the form for creating a new resource.
     * @return Response
     */
    public function create()
    {
        return view('silder::silder.create');
    }

    /**
     * Store a newly created resource in storage.
     * @param  Request $request
     * @return Response
     */
    public function store(Request $request,Silder $silder)
    {
       $post = $request->all();
       $img = [];
       $images = json_decode($request->input('images'));
       foreach($images as $v){
           array_push($img,$v);
       }
//       dd($img);
       $post = [
            'title' => $post['title'],
            'url' => $post['url'],
            'images' => json_encode($img),
            'is_show' => $post['is_show'],
        ];
//       dd($post);
       $silder->fill($post);
       $silder->save();
       return ['valid' => 1,'message' => '添加成功'];
    }

    /**
     * Show the specified resource.
     * @return Response
     */
    public function show()
    {
        return view('silder::show');
    }

    /**
     * Show the form for editing the specified resource.
     * @return Response
     */
    public function edit($id)
    {
        $data = Silder::find($id);
        return view('silder::silder.edit',compact('data'));
    }

    /**
     * Update the specified resource in storage.
     * @param  Request $request
     * @return Response
     */
    public function update(Request $request,$id)
    {
        $data = Silder::find($id);
        $data->title = $request->input('title');
        $data->url = $request->input('url');
        $data->images = $request->input('images');
        $data->is_show = $request->input('is_show');
        $data->save();
        return ['valid' => 1,'message' => '编辑成功'];
    }

    /**
     * Remove the specified resource from storage.
     * @return Response
     */
    public function destroy($id)
    {
        Silder::destroy($id);
        return ['valid' => 1,'message' => '删除成功'];
    }
}
